/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Codigo;

import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.util.List;
import javax.swing.ImageIcon;
import javax.swing.JOptionPane;

/**
 *
 * @author kyerte
 */
public class FrmDesarrollos extends javax.swing.JFrame {

    private ConsultaDesarrollos consultaDesarrollos;
    private List<Desarrollo> resultadoDesarrollos;
    private Desarrollo desarrolloActual;
    private int numeroDesarrollos;
    private int indiceActual;
    private int idDesarrollo;
    private boolean nuevoDesarrollo;

    public FrmDesarrollos() {
        initComponents();
        this.setIconImage(new ImageIcon(getClass().getResource("/Imagenes/Casa.png")).getImage());
        this.getContentPane().setBackground(new java.awt.Color(244, 243, 176));

        consultaDesarrollos = new ConsultaDesarrollos();
        explorarDesarrollos();
        
        txtNombre.addKeyListener(new KeyListener() {
            @Override
            public void keyTyped(KeyEvent e) {
                if (txtNombre.getText().length() == 45) {
                    e.consume();
                }
            }

            @Override
            public void keyPressed(KeyEvent arg0) {
            }

            @Override
            public void keyReleased(KeyEvent arg0) {
            }
        });
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        lblNombre = new javax.swing.JLabel();
        txtNombre = new javax.swing.JTextField();
        lblInformacion = new javax.swing.JLabel();
        jScrollPane1 = new javax.swing.JScrollPane();
        txtInfo = new javax.swing.JTextArea();
        cmdNuevo = new javax.swing.JButton();
        cmdGuardar = new javax.swing.JButton();
        cmdEliminar = new javax.swing.JButton();
        cmdCancelar = new javax.swing.JButton();
        jSeparator1 = new javax.swing.JSeparator();
        cmdPrimero = new javax.swing.JButton();
        cmdAnterior = new javax.swing.JButton();
        txtIndiceActual = new javax.swing.JTextField();
        jLabel1 = new javax.swing.JLabel();
        lblTotal = new javax.swing.JLabel();
        cmdSiguiente = new javax.swing.JButton();
        cmdUltimo = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Desarrollos");
        setResizable(false);

        lblNombre.setFont(new java.awt.Font("Ubuntu", 2, 18)); // NOI18N
        lblNombre.setText("Nombre");

        txtNombre.setFont(new java.awt.Font("Ubuntu", 2, 18)); // NOI18N

        lblInformacion.setFont(new java.awt.Font("Ubuntu", 2, 18)); // NOI18N
        lblInformacion.setText("Información");

        txtInfo.setColumns(20);
        txtInfo.setFont(new java.awt.Font("Ubuntu", 2, 18)); // NOI18N
        txtInfo.setRows(5);
        jScrollPane1.setViewportView(txtInfo);

        cmdNuevo.setBackground(new java.awt.Color(255, 255, 255));
        cmdNuevo.setFont(new java.awt.Font("Ubuntu", 2, 15)); // NOI18N
        cmdNuevo.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Imagenes/Nuevo.png"))); // NOI18N
        cmdNuevo.setMnemonic('n');
        cmdNuevo.setText("Nuevo");
        cmdNuevo.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cmdNuevoActionPerformed(evt);
            }
        });

        cmdGuardar.setBackground(new java.awt.Color(255, 255, 255));
        cmdGuardar.setFont(new java.awt.Font("Ubuntu", 2, 15)); // NOI18N
        cmdGuardar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Imagenes/Guardar.png"))); // NOI18N
        cmdGuardar.setMnemonic('g');
        cmdGuardar.setText("Guardar");
        cmdGuardar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cmdGuardarActionPerformed(evt);
            }
        });

        cmdEliminar.setBackground(new java.awt.Color(255, 255, 255));
        cmdEliminar.setFont(new java.awt.Font("Ubuntu", 2, 15)); // NOI18N
        cmdEliminar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Imagenes/Borrar.png"))); // NOI18N
        cmdEliminar.setMnemonic('e');
        cmdEliminar.setText("Eliminar");
        cmdEliminar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cmdEliminarActionPerformed(evt);
            }
        });

        cmdCancelar.setBackground(new java.awt.Color(255, 255, 255));
        cmdCancelar.setFont(new java.awt.Font("Ubuntu", 2, 15)); // NOI18N
        cmdCancelar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Imagenes/Cancelar.png"))); // NOI18N
        cmdCancelar.setMnemonic('c');
        cmdCancelar.setText("Cancelar");
        cmdCancelar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cmdCancelarActionPerformed(evt);
            }
        });

        cmdPrimero.setBackground(new java.awt.Color(255, 255, 255));
        cmdPrimero.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Imagenes/First.png"))); // NOI18N
        cmdPrimero.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cmdPrimeroActionPerformed(evt);
            }
        });

        cmdAnterior.setBackground(new java.awt.Color(255, 255, 255));
        cmdAnterior.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Imagenes/Back.png"))); // NOI18N
        cmdAnterior.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cmdAnteriorActionPerformed(evt);
            }
        });

        txtIndiceActual.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                txtIndiceActualActionPerformed(evt);
            }
        });

        jLabel1.setFont(new java.awt.Font("Ubuntu", 0, 18)); // NOI18N
        jLabel1.setText("/");

        lblTotal.setFont(new java.awt.Font("Ubuntu", 0, 24)); // NOI18N
        lblTotal.setText("100");

        cmdSiguiente.setBackground(new java.awt.Color(255, 255, 255));
        cmdSiguiente.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Imagenes/Last.png"))); // NOI18N
        cmdSiguiente.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cmdSiguienteActionPerformed(evt);
            }
        });

        cmdUltimo.setBackground(new java.awt.Color(255, 255, 255));
        cmdUltimo.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Imagenes/Forward.png"))); // NOI18N
        cmdUltimo.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cmdUltimoActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jSeparator1, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 514, Short.MAX_VALUE)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(cmdNuevo)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                .addComponent(cmdGuardar)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                .addComponent(cmdEliminar)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                .addComponent(cmdCancelar))
                            .addGroup(layout.createSequentialGroup()
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                                    .addComponent(lblNombre, javax.swing.GroupLayout.PREFERRED_SIZE, 68, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addComponent(lblInformacion))
                                .addGap(18, 18, 18)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addGroup(layout.createSequentialGroup()
                                        .addComponent(cmdPrimero)
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                        .addComponent(cmdAnterior)
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                        .addComponent(txtIndiceActual, javax.swing.GroupLayout.PREFERRED_SIZE, 46, javax.swing.GroupLayout.PREFERRED_SIZE)
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                        .addComponent(jLabel1)
                                        .addGap(9, 9, 9)
                                        .addComponent(lblTotal)
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                        .addComponent(cmdSiguiente)
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                        .addComponent(cmdUltimo))
                                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                        .addComponent(txtNombre, javax.swing.GroupLayout.DEFAULT_SIZE, 337, Short.MAX_VALUE)
                                        .addComponent(jScrollPane1)))))
                        .addGap(0, 0, Short.MAX_VALUE)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(35, 35, 35)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(txtNombre, javax.swing.GroupLayout.PREFERRED_SIZE, 38, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(lblNombre))
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(lblInformacion))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(14, 14, 14)
                        .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 22, Short.MAX_VALUE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(cmdAnterior, javax.swing.GroupLayout.PREFERRED_SIZE, 26, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                        .addComponent(lblTotal, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, 21, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(jLabel1))
                    .addComponent(cmdPrimero, javax.swing.GroupLayout.PREFERRED_SIZE, 26, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addComponent(cmdUltimo, javax.swing.GroupLayout.PREFERRED_SIZE, 26, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(cmdSiguiente, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, 0, Short.MAX_VALUE)
                            .addComponent(txtIndiceActual, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, 26, javax.swing.GroupLayout.PREFERRED_SIZE))))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jSeparator1, javax.swing.GroupLayout.PREFERRED_SIZE, 10, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(cmdGuardar, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, 0, Short.MAX_VALUE)
                    .addComponent(cmdEliminar, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, 0, Short.MAX_VALUE)
                    .addComponent(cmdCancelar, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, 34, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(cmdNuevo, javax.swing.GroupLayout.PREFERRED_SIZE, 34, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(23, 23, 23))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void cmdNuevoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cmdNuevoActionPerformed
        nuevoDesarrollo = true;
        txtNombre.setText("");
        txtInfo.setText("");
        cmdGuardar.setEnabled(true);
        cmdCancelar.setEnabled(true);
        cmdEliminar.setEnabled(false);
    }//GEN-LAST:event_cmdNuevoActionPerformed

    private void cmdGuardarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cmdGuardarActionPerformed
        cmdGuardar.setEnabled(true);
        cmdCancelar.setEnabled(true);

        String mensaje = "";

        int resultado = 0;

        if (nuevoDesarrollo) {
            resultado = consultaDesarrollos.agregarNuevoDesarrollo(txtNombre.getText(), txtInfo.getText());
            mensaje = "Se agregó nuevo desarrollo";
        } else {
            resultado = consultaDesarrollos.actualizarDesarrollo(txtNombre.getText(), txtInfo.getText(), idDesarrollo);
            mensaje = "Se actualizó desarrollo";
        }

        if (resultado == 1) {
            JOptionPane.showMessageDialog(null, mensaje,
                    "Desarrollos", JOptionPane.INFORMATION_MESSAGE);
        } else {
            JOptionPane.showMessageDialog(null, "No se pudo realizar la operación",
                    "Desarrollos", JOptionPane.ERROR_MESSAGE);
        }

        nuevoDesarrollo = false;
        explorarDesarrollos();

        cmdNuevo.setEnabled(true);
        cmdEliminar.setEnabled(true);
    }//GEN-LAST:event_cmdGuardarActionPerformed

    private void cmdPrimeroActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cmdPrimeroActionPerformed
        indiceActual = 0;
        txtIndiceActual.setText("" + (indiceActual + 1));
        txtIndiceActualActionPerformed(evt);
    }//GEN-LAST:event_cmdPrimeroActionPerformed

    private void txtIndiceActualActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_txtIndiceActualActionPerformed
        String str = txtIndiceActual.getText();

        if (ValidaNumeros.validarNumeroEntero(str)) {

            indiceActual = (Integer.parseInt(str) - 1);

            if (numeroDesarrollos != 0 && indiceActual < numeroDesarrollos) {

                desarrolloActual = resultadoDesarrollos.get(indiceActual);

                idDesarrollo = desarrolloActual.obtenerIdDesarrollo();
                txtNombre.setText(desarrolloActual.obtenerNombre());
                txtInfo.setText(desarrolloActual.obtenerDatos());
            }
        } else {
            JOptionPane.showMessageDialog(null, "Ingresa un número válido",
                    "Desarrollos", JOptionPane.ERROR_MESSAGE);
            explorarDesarrollos();
        }
    }//GEN-LAST:event_txtIndiceActualActionPerformed

    private void cmdAnteriorActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cmdAnteriorActionPerformed
        indiceActual--;

        if (indiceActual < 0) {
            indiceActual = numeroDesarrollos - 1;
        }

        txtIndiceActual.setText("" + (indiceActual + 1));
        txtIndiceActualActionPerformed(evt);
    }//GEN-LAST:event_cmdAnteriorActionPerformed

    private void cmdSiguienteActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cmdSiguienteActionPerformed
        indiceActual++;

        if (indiceActual >= numeroDesarrollos) {
            indiceActual = 0;
        }

        txtIndiceActual.setText("" + (indiceActual + 1));
        txtIndiceActualActionPerformed(evt);
    }//GEN-LAST:event_cmdSiguienteActionPerformed

    private void cmdUltimoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cmdUltimoActionPerformed
        indiceActual = numeroDesarrollos - 1;

        txtIndiceActual.setText("" + (indiceActual + 1));

        txtIndiceActualActionPerformed(evt);
    }//GEN-LAST:event_cmdUltimoActionPerformed

    private void cmdEliminarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cmdEliminarActionPerformed

        int opc = JOptionPane.showConfirmDialog(this, "¿Seguro que deseas eliminar este desarrollo?",
                "Desarrollos", JOptionPane.YES_NO_OPTION);

        int resultado = 0;

        if (opc == JOptionPane.YES_OPTION) {


            resultado = consultaDesarrollos.eliminarDesarrollo(idDesarrollo);

            if (resultado != 0) {

                JOptionPane.showMessageDialog(null, "Desarrollo eliminado",
                        "Desarrollo", JOptionPane.INFORMATION_MESSAGE);

                explorarDesarrollos();

            } else {
                JOptionPane.showMessageDialog(null, "No se pudo eliminar desarrollo",
                        "Desarrollos", JOptionPane.INFORMATION_MESSAGE);
            }
        }

        cmdNuevo.setEnabled(true);
        cmdGuardar.setEnabled(true);
        cmdCancelar.setEnabled(true);
        cmdEliminar.setEnabled(true);
    }//GEN-LAST:event_cmdEliminarActionPerformed

    private void cmdCancelarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cmdCancelarActionPerformed
        explorarDesarrollos();
        nuevoDesarrollo = false;
        cmdNuevo.setEnabled(true);
        cmdGuardar.setEnabled(true);
        cmdCancelar.setEnabled(true);
        cmdEliminar.setEnabled(true);
    }//GEN-LAST:event_cmdCancelarActionPerformed
    private void explorarDesarrollos() {
        resultadoDesarrollos = consultaDesarrollos.consultarDesarrollos();
        numeroDesarrollos = resultadoDesarrollos.size();

        if (numeroDesarrollos != 0) {

            indiceActual = 0;

            desarrolloActual = resultadoDesarrollos.get(indiceActual);
            idDesarrollo = desarrolloActual.obtenerIdDesarrollo();
            txtNombre.setText(desarrolloActual.obtenerNombre());
            txtInfo.setText(desarrolloActual.obtenerDatos());

            txtIndiceActual.setText("" + (indiceActual + 1));
            lblTotal.setText("" + numeroDesarrollos);

        } else {
            cmdGuardar.setEnabled(false);
            cmdCancelar.setEnabled(false);
            cmdEliminar.setEnabled(false);
        }
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton cmdAnterior;
    private javax.swing.JButton cmdCancelar;
    private javax.swing.JButton cmdEliminar;
    private javax.swing.JButton cmdGuardar;
    private javax.swing.JButton cmdNuevo;
    private javax.swing.JButton cmdPrimero;
    private javax.swing.JButton cmdSiguiente;
    private javax.swing.JButton cmdUltimo;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JSeparator jSeparator1;
    private javax.swing.JLabel lblInformacion;
    private javax.swing.JLabel lblNombre;
    private javax.swing.JLabel lblTotal;
    private javax.swing.JTextField txtIndiceActual;
    private javax.swing.JTextArea txtInfo;
    private javax.swing.JTextField txtNombre;
    // End of variables declaration//GEN-END:variables
}
